/** 忘记密码 v1.0.0 **/
//步骤器标题
var stepData = kaValue.forgotPassword.stepData;
var stepOptions = {};
var sliderVerifyBoolean = false;
layui.use(kaValue.layuiUse, function () {
    //获取当前租户
    var currTenantId = $.getUrlParam('currTenantId');
    if (currTenantId != undefined && currTenantId != null && currTenantId != "") {
        $("#tenant").val(decodeURIComponent(decodeURI(currTenantId)));
    }
    var currTenantName = $.getUrlParam('currTenantName');
    if (currTenantName != undefined && currTenantName != null && currTenantName != "") {
        $("#forgotPasswordFormCurrTenant").html(decodeURIComponent(decodeURI(currTenantName)));
        $("#forgotPasswordFormCurrTenant_2").html(decodeURIComponent(decodeURI(currTenantName)));
        $("#forgotPasswordFormCurrTenant_3").html(decodeURIComponent(decodeURI(currTenantName)));
    }
    //步骤
    var step = layui.step;
    stepOptions = {
        elem: '#forgotPasswordDiv',
        data: stepData,
        width: '230px',
    }
    step.ready(stepOptions)
    //初始化
    $("#forgotPasswordDiv_1").hide();
    $("#forgotPasswordDiv_2").hide();
    $("#forgotPasswordDiv_3").hide();
    $("#forgotPasswordDiv_4").hide();
    //显示第一步
    $("#forgotPasswordDiv_1").show();
    forgotPasswordFun.forgotPasswordImage();
    //绑定按钮 显示第二步
    $("#forgotPasswordDiv_1_button").click(function () {
        forgotPasswordFun.getForgotPasswordUserInfor(function (re) {
            $("#username_2").html(re.data.userName);
            $("#userid").val(re.data.userId);
            $("#lastVerificationId").val(re.lastVerificationId);
            $("#phonenumber_2").html(re.data.phonenumber);
            $("#username_3").html(re.data.userName);
            $("#forgotPasswordDiv_1").hide();
            $("#forgotPasswordDiv_2").show();
            stepData[0].status = "1";
            stepData[1].status = "3";
            stepOptions.data = stepData;
            step.ready(stepOptions);
        });
    });
    //显示第三步
    $("#forgotPasswordDiv_2_button").click(function () {
        forgotPasswordFun.authentication(function (re) {
            $("#lastLastVerificationId").val(re.lastLastVerificationId);
            var phonenumber = $("#phonenumber").val();
            $("#phonenumber_3").html(phonenumber);
            $("#forgotPasswordDiv_1").hide();
            $("#forgotPasswordDiv_2").hide();
            $("#forgotPasswordDiv_3").show();
            stepData[0].status = "1";
            stepData[1].status = "1";
            stepData[2].status = "3";
            stepOptions.data = stepData;
            step.ready(stepOptions);
        });
    });
    //显示第四步
    $("#forgotPasswordDiv_3_button").click(function () {
        forgotPasswordFun.fWUpdateNewPassWord(function (re) {
            $("#forgotPasswordDiv_1").hide();
            $("#forgotPasswordDiv_2").hide();
            $("#forgotPasswordDiv_3").hide();
            $("#forgotPasswordDiv_4").show();
            stepData[0].status = "1";
            stepData[1].status = "1";
            stepData[2].status = "1";
            stepData[3].status = "1";
            stepOptions.data = stepData;
            step.ready(stepOptions);
        });
    });
});
var forgotPasswordFun = { // 忘记密码方法
    forgotPasswordImage: function () { //验证码
        var captchaImageOption = kaValue.getUrl(kaValue.forgotPassword.url.forgotPasswordImage);// 忘记密码验证码
        captchaImageOption.success = function (re) {
            if (re.code == 200) {
                var image = "data:image/gif;base64," + re.img;
                $("#forgotPasswordImagesMsg").attr("src", image);
                $("#uuid").val(re.uuid);
            }
        }
        ka.ajax(captchaImageOption);
    },
    getForgotPasswordUserInfor: function (callback) {// 获取用户详细信息 
        var tenant = $("#tenant").val();//租户 
        var username = $("#username").val();//用户名 
        var verCode = $("#verCode").val();//验证码
        var uuid = $("#uuid").val();//uuid
        var option = kaValue.getUrl(kaValue.forgotPassword.url.getForgotPasswordUserInfor);
        if (tenant == undefined || tenant == null || tenant == "") {
            kaMsg.error({ "code": "500", "msg": kaValue.forgotPassword.msg.msg1}); //租户不能为空！
            return false;
        }
        if (username == undefined || username == null || username == "") {
            kaMsg.error({ "code": "500", "msg":  kaValue.forgotPassword.msg.msg2});//用户名不能为空！
            return false;
        }
        if (verCode == undefined || verCode == null || verCode == "") {
            kaMsg.error({ "code": "500", "msg":  kaValue.forgotPassword.msg.getForgotPasswordUserInforMsg3});//验证码不能为空！
            return false;
        }
        if (uuid == undefined || uuid == null || uuid == "") {
            kaMsg.error({ "code": "500", "msg":  kaValue.forgotPassword.msg.msg3});//未获取验证码！
            return false;
        }
        option.data = {
            "tenant": tenant,
            "username": username,
            "verCode": verCode,
            "uuid": uuid
        };
        option.success = function (re) {
            if (re.code == 200) {
                if (callback != undefined && callback != null && callback != "") {
                    callback(re);
                }
            } else {
                kaMsg.error({ "code": re.code, "msg": re.msg });
            }
        }
        ka.ajax(option);
    },
    authentication: function (callback) { //验证码身份
        var tenant = $("#tenant").val();//租户 
        var userid = $("#userid").val();//用户ID
        var username = $("#username").val();//用户名 
        var lastVerificationId = $("#lastVerificationId").val();//唯一识别码
        var phonenumber = $("#phonenumber").val();//手机号码
        var codePhonenumber = $("#codePhonenumber").val();//手机号码
        var uuid = $("#uuid").val();//uuid
        if (tenant == undefined || tenant == null || tenant == "") {
            kaMsg.error({ "code": "500", "msg":  kaValue.forgotPassword.msg.msg1});//租户不能为空！
            return false;
        }
        if (username == undefined || username == null || username == "") {
            kaMsg.error({ "code": "500", "msg":  kaValue.forgotPassword.msg.msg2});//用户名不能为空！
            return false;
        }
        if (userid == undefined || userid == null || userid == "") {
            kaMsg.error({ "code": "500", "msg": kaValue.forgotPassword.msg.msg4});//用户ID！
            return false;
        }
        if (uuid == undefined || uuid == null || uuid == "") {
            kaMsg.error({ "code": "500", "msg":  kaValue.forgotPassword.msg.msg3}); //未获取验证码！
            return false;
        }
        if (lastVerificationId == undefined || lastVerificationId == null || lastVerificationId == "") {
            kaMsg.error({ "code": "500", "msg": kaValue.forgotPassword.msg.authenticationMsg2}); //无效的上次识别码！
            return false;
        }
        if (phonenumber == undefined || phonenumber == null || phonenumber == "") {
            kaMsg.error({ "code": "500", "msg": kaValue.forgotPassword.msg.msg5}); //手机号码不能为空！
            return false;
        }
        if(!(/^1\d{10}$/.test(phonenumber))){
            kaMsg.error({ "code": "500", "msg": kaValue.forgotPassword.msg.authenticationMsg4}); //手机号码不正确！
            return false;
        }
        if (codePhonenumber == undefined || codePhonenumber == null || codePhonenumber == "") {
            kaMsg.error({ "code": "500", "msg": kaValue.forgotPassword.msg.authenticationMsg5}); //手机号码编码不能为空！
            return false;
        }
        var option = kaValue.getUrl(kaValue.forgotPassword.url.authentication); //验证码身份
        option.data = {
            "tenant": tenant,
            "username": username,
            "userid": userid,
            "lastVerificationId": lastVerificationId,
            "phonenumber": phonenumber,
            "codePhonenumber": codePhonenumber,
            "uuid": uuid
        }
        option.success = function (re) {
            if (re.code == 200) {
                if (callback != undefined && callback != null && callback != "") {
                    callback(re);
                }
            } else {
                kaMsg.error({ "code": re.code, "msg": re.msg });
            }
        }
        ka.ajax(option);
    },
    fWUpdateNewPassWord: function (callback) { // 更新密码
        var tenant = $("#tenant").val(); // 租户 
        var userid = $("#userid").val(); // 用户ID
        var username = $("#username").val(); // 用户名 
        var phonenumber = $("#phonenumber").val(); // 手机号码
        var lastLastVerificationId = $("#lastLastVerificationId").val(); // 上次id
        var newpassword = $("#newpassword").val(); // 密码
        if (tenant == undefined || tenant == null || tenant == "") {
            kaMsg.error({ "code": "500", "msg": kaValue.forgotPassword.msg.msg1}); // 租户不能为空！
            return false;
        }
        if (username == undefined || username == null || username == "") {
            kaMsg.error({ "code": "500", "msg": kaValue.forgotPassword.msg.msg2}); // 用户名不能为空！
            return false;
        }
        if (userid == undefined || userid == null || userid == "") {
            kaMsg.error({ "code": "500", "msg": kaValue.forgotPassword.msg.msg4});// 用户ID！
            return false;
        }
        if (phonenumber == undefined || phonenumber == null || phonenumber == "") {
            kaMsg.error({ "code": "500", "msg": kaValue.forgotPassword.msg.msg5});// 手机号码不能为空！
            return false;
        }
        if (lastLastVerificationId == undefined || lastLastVerificationId == null || lastLastVerificationId == "") {
            kaMsg.error({ "code": "500", "msg": kaValue.forgotPassword.msg.fWUpdateNewPassWord2});// 上一次验证ID不能为空！
            return false;
        }
        if (newpassword == undefined || newpassword == null || newpassword == "") {
            kaMsg.error({ "code": "500", "msg": kaValue.forgotPassword.msg.fWUpdateNewPassWord3});//密码不能为空！
            return false;
        }
        var option = kaValue.getUrl(kaValue.forgotPassword.url.fWUpdateNewPassWord);// 忘记密码 更新新密码
        option.data = {
            "tenant": tenant, //租户
            "username": username, //用户名
            "userid": userid, //用户ID
            "phonenumber": phonenumber, //手机号码
            "lastLastVerificationId": lastLastVerificationId, //上一次验证ID
            "newpassword": newpassword//新密码
        }
        option.success = function (re) {
            if (re.code == 200) {
                if (callback != undefined && callback != null && callback != "") {
                    callback(re);
                }
            } else {
                kaMsg.error({ "code": re.code, "msg": re.msg });
            }
        }
        ka.ajax(option);
    },
    getPhonenVerCode: function () { //获取手机验证码
        var phonenumber = $("#phonenumber").val();
        if (phonenumber != undefined && phonenumber != null && phonenumber != "") {
            var regMobile = kaValue.regMobile;//手机验证规则
            if (!regMobile.test(phonenumber)) {
                var option = {
                    code: "500",
                    msg: kaValue.forgotPassword.msg.getPhonenVerCodeMsg1 //请输入正确的手机号
                }
                kaMsg.error(option);
                return false;
            }
        } else {
            var option = {
                code: "500",
                msg: kaValue.forgotPassword.msg.getPhonenVerCodeMsg2 // 请填写手机号码!
            }
            kaMsg.error(option);
            return false;
        }
        forgotPasswordFun.sliderVerify();
        if (sliderVerifyBoolean) {
            sliderVerifyBoolean = false;
            forgotPasswordFun.PhonenVerCodeButtonFun(1);
            var tenant = $("#tenant").val();
            var option = kaValue.getUrl(kaValue.forgotPassword.url.getPhonenVerCode);// 
            option.data = {
                "phonenumber": phonenumber, //手机号码
                "type": 2, // 类型
                "tenant": tenant //租户
            };
            option.success = function (re) {
                if (re.code == 200) {
                    kaMsg.success(re);
                } else {
                    kaMsg.error(re);
                }
            }
            ka.ajax(option);
        }
    },
    PhonenVerCodeButtonFun: function (types) {// 获取验证按钮
        if (types == 1) {
            var PhonenVerCodeButtonNumberStr = sessionStorage.getItem("PhonenVerCodeButtonNumber");
            if (PhonenVerCodeButtonNumberStr == undefined || PhonenVerCodeButtonNumberStr == null || PhonenVerCodeButtonNumberStr == "") {
                PhonenVerCodeButtonNumberStr = "60";
            }
            var PhonenVerCodeButtonNumber = parseInt(PhonenVerCodeButtonNumberStr);
            if (PhonenVerCodeButtonNumber > 0) {
                $("#PhonenVerCodeButton").html(PhonenVerCodeButtonNumber);
                $("#PhonenVerCodeButton").addClass("layui-btn-disabled");
                $("#PhonenVerCodeButton").prop('disabled', true); // 禁用按钮
                setTimeout(function () {
                    sessionStorage.setItem("PhonenVerCodeButtonNumber", (PhonenVerCodeButtonNumber - 1));
                    forgotPasswordFun.PhonenVerCodeButtonFun(1);
                }, 1000);
            } else {
                $("#PhonenVerCodeButton").html("获取");
                $("#PhonenVerCodeButton").removeClass("layui-btn-disabled");
                $("#PhonenVerCodeButton").removeAttr('disabled');
                sessionStorage.removeItem("PhonenVerCodeButtonNumber");
            }
        } else {
            var PhonenVerCodeButtonNumberStr = sessionStorage.getItem("PhonenVerCodeButtonNumber");
            if (!(PhonenVerCodeButtonNumberStr == undefined || PhonenVerCodeButtonNumberStr == null || PhonenVerCodeButtonNumberStr == "")) {
                var PhonenVerCodeButtonNumber = parseInt(PhonenVerCodeButtonNumberStr);
                if (PhonenVerCodeButtonNumber > 0) {
                    $("#PhonenVerCodeButton").html(PhonenVerCodeButtonNumber);
                    $("#PhonenVerCodeButton").addClass("layui-btn-disabled");
                    $("#PhonenVerCodeButton").prop('disabled', true); // 禁用按钮
                    setTimeout(function () {
                        sessionStorage.setItem("PhonenVerCodeButtonNumber", (PhonenVerCodeButtonNumber - 1));
                        forgotPasswordFun.PhonenVerCodeButtonFun(1);
                    }, 1000);
                }
            }
        }
    },
    sliderVerify: function () { // 打开点击验证码
        if (!sliderVerifyBoolean) {
            var commonAdGetSliderVerify = [];
            var option = kaValue.getUrl(kaValue.forgotPassword.url.commonAdGetSliderVerify);
            option.async = false;
            option.success = function (re) {
                if (re.code == 200) {
                    commonAdGetSliderVerify = re.data;
                }
            }
            ka.ajax(option);
            var layerIndex = layer.open({
                type: 1,
                area: ['304px', '376px'], // 宽高
                title: false, // 不显示标题栏
                closeBtn: 0,
                shade: [0.6, '#FFF'], // 点击遮罩区域，关闭弹层
                shadeClose: false, // 点击遮罩关闭层
                content: `<div id="sliderVerify"></div>`,
                success: function (layero, index, that) {
                    $('#sliderVerify').pointsVerify({
                        defaultNum: 5, //默认的文字数量
                        checkNum: 4, //校对的文字数量
                        vSpace: 4, //间隔
                        imgName: commonAdGetSliderVerify,
                        imgSize: {
                            width: '280px',
                            height: '290px',
                        },
                        barSize: {
                            width: '90%',
                            height: '40px',
                        },
                        ready: function () { },
                        success: function () {
                            sliderVerifyBoolean = true;
                            layui.layer.closeAll();
                            forgotPasswordFun.getPhonenVerCode();
                        },
                        error: function () {
                            sliderVerifyBoolean = false;
                            kalog.outLogs(["sliderVerify生成错误", {}]);
                        }
                    });
                    $(".verify-close").click(function () {
                        layui.layer.close(layerIndex);
                    });
                }
            });
        }
    }
}
